<!-- HTML header for doxygen 1.8.13-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>MTB CAT1 Peripheral driver library: Direct Crypto Core Access</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><a href="http://www.cypress.com/"><img alt="Logo" src="IFXCYP_one-line.png"/></a></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">MTB CAT1 Peripheral driver library</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('group__group__crypto__lld__api.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="summary">
<a href="#groups">API Reference</a>  </div>
  <div class="headertitle">
<div class="title">Direct Crypto Core Access<div class="ingroups"><a class="el" href="group__group__crypto.html">Crypto       (Cryptography)</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<a name="details" id="details"></a><h2 class="groupheader">General Description</h2>
<p>Use the low-level API for direct access to the Crypto hardware. </p>
<p>The functions and other declarations used in this part of the driver are in cy_crypto_core.h. You can also include cy_pdl.h to get access to all functions and declarations in the PDL.</p>
<p>Firmware initializes and starts the Crypto operations. The firmware then provides the configuration data required for the desired cryptographic technique.</p>
<h1><a class="anchor" id="group_crypto_lld_common_use_cases"></a>
Common Use Cases</h1>
<h2><a class="anchor" id="group_crypto_lld_crypto_enable"></a>
Crypto Hardware Enable</h2>
<p>Use <a class="el" href="group__group__crypto__lld__hw__functions.html#ga6aede3754a84e01d5e9b4227a683c24c">Cy_Crypto_Core_Enable</a> to enable the Crypto IP block, After this call, the Crypto driver is ready to execute crypto functions</p>
<p>Code example: </p><div class="fragment"><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line">    <span class="comment">/* Enable Crypto IP */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__hw__functions.html#ga6aede3754a84e01d5e9b4227a683c24c">Cy_Crypto_Core_Enable</a>(</div><div class="line">                             CRYPTO_HW);  <span class="comment">/* Pointer to Crypto instance */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_crypto_disable"></a>
Crypto Hardware Disable</h2>
<p>Use <a class="el" href="group__group__crypto__lld__hw__functions.html#gaa25e439d33aee00d542476a82720e5cd">Cy_Crypto_Core_Disable</a> to disable the Crypto IP block and call <a class="el" href="group__group__crypto__lld__hw__functions.html#ga000bf3a63e4f2ee9fb8bb3a452b87d87">Cy_Crypto_Core_ClearVuRegisters</a> to clear whole register file, After this call, No crypto function should be executed</p>
<p>Code example: </p><div class="fragment"><div class="line"></div><div class="line">    <span class="comment">/* Check if Crypto IP is enabled */</span></div><div class="line">    <span class="keywordflow">if</span> (<a class="code" href="group__group__crypto__lld__hw__functions.html#gab4a5ca67ee625db608b6380fec04a6a3">Cy_Crypto_Core_IsEnabled</a>(CRYPTO_HW)) {</div><div class="line"></div><div class="line">        <span class="comment">/* Disable Crypto HW */</span></div><div class="line">        <a class="code" href="group__group__crypto__lld__hw__functions.html#gaa25e439d33aee00d542476a82720e5cd">Cy_Crypto_Core_Disable</a>(CRYPTO_HW);</div><div class="line"></div><div class="line">        <span class="comment">/* Clear whole register file */</span></div><div class="line">        <a class="code" href="group__group__crypto__lld__hw__functions.html#ga000bf3a63e4f2ee9fb8bb3a452b87d87">Cy_Crypto_Core_ClearVuRegisters</a>(CRYPTO_HW);</div><div class="line">    }</div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_Use_TDES"></a>
TDES encryption</h2>
<p>To encrypt a message using the TDES algorithm:</p>
<ul>
<li>Place three DES keys into an array,</li>
<li>Call <a class="el" href="group__group__crypto__lld__symmetric__functions.html#ga46e4f33120f1f9b5f1858da65917f267">Cy_Crypto_Core_Tdes</a> with required parameters, including the array of keys</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line">    <span class="comment">/* Note: all keys are placed into the one keys array. */</span></div><div class="line"></div><div class="line"><span class="preprocessor">    #define STRING_LENGTH  8</span></div><div class="line">    uint8_t msgString[STRING_LENGTH] = <span class="stringliteral">&quot;Crypto!&quot;</span>;</div><div class="line">    uint8_t encString[STRING_LENGTH];</div><div class="line"></div><div class="line"><span class="preprocessor">    #define KEY1 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77</span></div><div class="line"><span class="preprocessor">    #define KEY2 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF</span></div><div class="line"><span class="preprocessor">    #define KEY3 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08</span></div><div class="line"></div><div class="line">    uint8_t tdesKey[] = { KEY1, KEY2, KEY3 };</div><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__symmetric__functions.html#ga46e4f33120f1f9b5f1858da65917f267">Cy_Crypto_Core_Tdes</a>(</div><div class="line">                             CRYPTO_HW,</div><div class="line">                             <a class="code" href="group__group__crypto__enums.html#ggae040fb530d4d110976bedfcdb7c8f09ca4b95ef1e3f73771b0c6a376c6d43e16d">CY_CRYPTO_ENCRYPT</a>,</div><div class="line">                             tdesKey,    <span class="comment">/* Pointer to keys */</span></div><div class="line">                             encString,  <span class="comment">/* Destination string */</span></div><div class="line">                             msgString); <span class="comment">/* Source String */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_Use_DES"></a>
DES encryption</h2>
<p>To encrypt a message using the DES algorithm:</p><ul>
<li>Place DES key into an array,</li>
<li>Call <a class="el" href="group__group__crypto__lld__symmetric__functions.html#ga34af8cf89ed2ebfb51941dce5c7c43c4">Cy_Crypto_Core_Des</a> with required parameters, including the key array</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line">    <span class="comment">/* Note: all keys are placed into the one keys array. */</span></div><div class="line"></div><div class="line"><span class="preprocessor">    #define STRING_LENGTH  8</span></div><div class="line">    uint8_t msgString[STRING_LENGTH] = <span class="stringliteral">&quot;Crypto!&quot;</span>;</div><div class="line">    uint8_t encString[STRING_LENGTH];</div><div class="line"></div><div class="line"><span class="preprocessor">    #define KEY1 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77</span></div><div class="line"></div><div class="line">    uint8_t desKey[] = { KEY1 };</div><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__symmetric__functions.html#ga34af8cf89ed2ebfb51941dce5c7c43c4">Cy_Crypto_Core_Des</a>(</div><div class="line">                             CRYPTO_HW,</div><div class="line">                             <a class="code" href="group__group__crypto__enums.html#ggae040fb530d4d110976bedfcdb7c8f09ca4b95ef1e3f73771b0c6a376c6d43e16d">CY_CRYPTO_ENCRYPT</a>,</div><div class="line">                             desKey,     <span class="comment">/* Pointer to key */</span></div><div class="line">                             encString,  <span class="comment">/* Destination string */</span></div><div class="line">                             msgString); <span class="comment">/* Source String */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_Use_AES"></a>
AES encryption</h2>
<p>The Crypto driver provides a four AES encryption algorithms (ECB, CBC, CFB and CTR) that are used similarly.</p>
<p>To encrypt a message using AES ECB algorithm (as example):</p><ul>
<li>Place AES key into array of appropriate size</li>
<li>Use <a class="el" href="group__group__crypto__lld__symmetric__functions.html#gaea56d7bae9d75b93886094f248e26006">Cy_Crypto_Core_Aes_Init</a> to configure the operation</li>
<li>Call <a class="el" href="group__group__crypto__lld__symmetric__functions.html#ga2b6e50fe58b6e41aa47b8a588808dc76">Cy_Crypto_Core_Aes_Ecb</a> (<a class="el" href="group__group__crypto__lld__symmetric__functions.html#ga3410c8e8bc02e192a2d9a371a00d2c4a">Cy_Crypto_Core_Aes_Cbc</a>, <a class="el" href="group__group__crypto__lld__symmetric__functions.html#ga050212232c6625a29c2e91efac199841">Cy_Crypto_Core_Aes_Cfb</a> or <a class="el" href="group__group__crypto__lld__symmetric__functions.html#gac62e5c170a6f7d18d67d2c932449d87f">Cy_Crypto_Core_Aes_Ctr</a>) with appropriate parameters to make an operation</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line"></div><div class="line">    uint8_t aesKey[<a class="code" href="group__group__crypto__cli__srv__macros.html#ga0170aa17289178203dcc2114a31c400e">CY_CRYPTO_AES_128_KEY_SIZE</a>] = {</div><div class="line">        0x2Bu, 0x7Eu, 0x15u, 0x16u, 0x28u, 0xAEu, 0xD2u, 0xA6u,</div><div class="line">        0xABu, 0xF7u, 0x15u, 0x88u, 0x09u, 0xCFu, 0x4Fu, 0x3Cu</div><div class="line">    };</div><div class="line"></div><div class="line">    uint8_t aesEcbPlainText[<a class="code" href="group__group__crypto__cli__srv__macros.html#gab966285a569cc381f21ca7ebb9918128">CY_CRYPTO_AES_BLOCK_SIZE</a>]  = {</div><div class="line">        0x6Bu, 0xC0u, 0xBCu, 0xE1u, 0x2Au, 0x45u, 0x99u, 0x91u,</div><div class="line">        0xE1u, 0x34u, 0x74u, 0x1Au, 0x7Fu, 0x9Eu, 0x19u, 0x25u</div><div class="line">    };</div><div class="line"></div><div class="line">    uint8_t aesEcbCipherText[<a class="code" href="group__group__crypto__cli__srv__macros.html#gab966285a569cc381f21ca7ebb9918128">CY_CRYPTO_AES_BLOCK_SIZE</a>] = {0};</div><div class="line"></div><div class="line">    <a class="code" href="structcy__stc__crypto__aes__state__t.html">cy_stc_crypto_aes_state_t</a> aesState;</div><div class="line">        <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line">    <span class="comment">/* Initialize Crypto AES functionality  */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__symmetric__functions.html#gaea56d7bae9d75b93886094f248e26006">Cy_Crypto_Core_Aes_Init</a>(</div><div class="line">                             CRYPTO_HW,</div><div class="line">                             aesKey,                <span class="comment">/* Pointer to key */</span></div><div class="line">                             <a class="code" href="group__group__crypto__enums.html#gga5402841d4798c0a82d41d8433c92b62fa1d7e508252e541450df0eb37f4c0bf6e">CY_CRYPTO_KEY_AES_128</a>, <span class="comment">/* Key size */</span></div><div class="line">                             &amp;aesState); <span class="comment">/* pointer to AES state structure */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div><div class="line">    <span class="comment">/* Encrypt one block (16 Bytes) by AES128 */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__symmetric__functions.html#ga2b6e50fe58b6e41aa47b8a588808dc76">Cy_Crypto_Core_Aes_Ecb</a>(</div><div class="line">                             CRYPTO_HW,</div><div class="line">                             <a class="code" href="group__group__crypto__enums.html#ggae040fb530d4d110976bedfcdb7c8f09ca4b95ef1e3f73771b0c6a376c6d43e16d">CY_CRYPTO_ENCRYPT</a>,</div><div class="line">                             aesEcbCipherText, <span class="comment">/* destination block */</span></div><div class="line">                             aesEcbPlainText,  <span class="comment">/* source block */</span></div><div class="line">                             &amp;aesState);</div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div><div class="line">    <span class="comment">/* Clear AES operation context */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__symmetric__functions.html#ga6175ad8a73bb13bb2baea32c5e26239a">Cy_Crypto_Core_Aes_Free</a>(</div><div class="line">                             CRYPTO_HW,</div><div class="line">                             &amp;aesState);</div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_Use_CRC"></a>
CRC Calculation</h2>
<p>To calculate CRC of a data image:</p><ul>
<li>Use <a class="el" href="group__group__crypto__lld__crc__functions.html#ga881e13920b637d25be9d8f98f5b10c91">Cy_Crypto_Core_Crc_CalcInit</a> to set parameters for selected CRC mode,</li>
<li>Call <a class="el" href="group__group__crypto__lld__crc__functions.html#ga4f97802fcf9f38711103e24f1ad9c584">Cy_Crypto_Core_Crc_Calc</a> to calculate CRC for a data image.</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line">    <span class="comment">/* CRC parameters for some CRC algorithms:</span></div><div class="line"><span class="comment">    +---------------------+-----+------------+------------+------+------+-----+------------+------------+</span></div><div class="line"><span class="comment">    | CRC algorithm Name  | CRC |  Polynom   |   Initial  | Data | Data | Rem |  Remainder |  Expected  |</span></div><div class="line"><span class="comment">    |                     | len |            |    seed    | REV  | XOR  | REV |    XOR     |    CRC     |</span></div><div class="line"><span class="comment">    | ------------------- | --- | ---------- |----------- | ---- | ---- | --- | ---------- | ---------- |</span></div><div class="line"><span class="comment">    | CRC-6  / CDMA2000-A |  6  |    0x27    |    0x3F    |   0  |   0  |  0  |    0x00    |    0x0D    |</span></div><div class="line"><span class="comment">    | CRC-6  / CDMA2000-B |  6  |    0x07    |    0x3F    |   0  |   0  |  0  |    0x00    |    0x3B    |</span></div><div class="line"><span class="comment">    | CRC-6  / DARC       |  6  |    0x19    |    0x00    |   1  |   0  |  1  |    0x00    |    0x26    |</span></div><div class="line"><span class="comment">    | CRC-6  / ITU        |  6  |    0x03    |    0x00    |   1  |   0  |  1  |    0x00    |    0x06    |</span></div><div class="line"><span class="comment">    | CRC-8  / ITU        |  8  |    0x07    |    0x00    |   0  |   0  |  0  |    0x55    |    0xA1    |</span></div><div class="line"><span class="comment">    | CRC-8  / MAXIM      |  8  |    0x31    |    0x00    |   1  |   0  |  1  |    0x00    |    0xA1    |</span></div><div class="line"><span class="comment">    | CRC-8  / ROHC       |  8  |    0x07    |    0xFF    |   1  |   0  |  1  |    0x00    |    0xD0    |</span></div><div class="line"><span class="comment">    | CRC-8  / WCDMA      |  8  |    0x9B    |    0x00    |   1  |   0  |  1  |    0x00    |    0x25    |</span></div><div class="line"><span class="comment">    | CRC-16 / CCITT-0    | 16  |   0x1021   |   0xFFFF   |   0  |   0  |  0  |   0x0000   |   0x29B1   |</span></div><div class="line"><span class="comment">    | CRC-16 / CDMA2000   | 16  |   0xC867   |   0xFFFF   |   0  |   0  |  0  |   0x0000   |   0x4C06   |</span></div><div class="line"><span class="comment">    | CRC-32              | 32  | 0x04C11DB7 | 0xFFFFFFFF |   1  |   0  |  1  | 0xFFFFFFFF | 0xCBF43926 |</span></div><div class="line"><span class="comment">    | CRC-32 / BZIP2      | 32  | 0x04C11DB7 | 0xFFFFFFFF |   0  |   0  |  0  | 0xFFFFFFFF | 0xFC891918 |</span></div><div class="line"><span class="comment">    +---------------------+-----+------------+------------+------+------+-----+------------+------------+</span></div><div class="line"><span class="comment">    */</span></div><div class="line"></div><div class="line">    <span class="comment">/* Use &quot;CRC-16/CCITT-0&quot; calculation */</span></div><div class="line"><span class="preprocessor">    #define CRC16CCITT_WIDTH           (16u)</span></div><div class="line"><span class="preprocessor">    #define CRC16CCITT_POLYNOMIAL      (0x1021)</span></div><div class="line"><span class="preprocessor">    #define CRC16CCITT_LFSR_SEED       (0xffff)</span></div><div class="line"><span class="preprocessor">    #define CRC16CCITT_DATA_REVERSE    (0u)</span></div><div class="line"><span class="preprocessor">    #define CRC16CCITT_DATA_XOR        (0u)</span></div><div class="line"><span class="preprocessor">    #define CRC16CCITT_REM_REVERSE     (0u)</span></div><div class="line"><span class="preprocessor">    #define CRC16CCITT_REM_XOR         (0x0000)</span></div><div class="line"></div><div class="line">    uint8_t message[9] = <span class="stringliteral">&quot;123456789&quot;</span>;</div><div class="line">    uint32_t calculatedCrc = 0;</div><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line">    <span class="comment">/* Initialization Crypto operation */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__crc__functions.html#ga881e13920b637d25be9d8f98f5b10c91">Cy_Crypto_Core_Crc_CalcInit</a>(</div><div class="line">                             CRYPTO_HW,</div><div class="line">                             CRC16CCITT_WIDTH,</div><div class="line">                             CRC16CCITT_POLYNOMIAL,</div><div class="line">                             CRC16CCITT_DATA_REVERSE,</div><div class="line">                             CRC16CCITT_DATA_XOR,</div><div class="line">                             CRC16CCITT_REM_REVERSE,</div><div class="line">                             CRC16CCITT_REM_XOR,</div><div class="line">                             CRC16CCITT_LFSR_SEED);</div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div><div class="line">    <span class="comment">/* Calculate CRC */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__crc__functions.html#ga4f97802fcf9f38711103e24f1ad9c584">Cy_Crypto_Core_Crc_Calc</a>(</div><div class="line">                             CRYPTO_HW,         <span class="comment">/* Pointer to Crypto instance */</span></div><div class="line">                             CRC16CCITT_WIDTH,            <span class="comment">/* CRC width in bits*/</span></div><div class="line">                             &amp;calculatedCrc,         <span class="comment">/* Pointer to CRC result */</span></div><div class="line">                             (<span class="keywordtype">void</span>*)message,            <span class="comment">/* Pointer to message */</span></div><div class="line">                             <span class="keyword">sizeof</span>(message));             <span class="comment">/* Size of message */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_Use_SHA"></a>
SHA digest calculation</h2>
<p>To calculate a SHA digest of a message:</p><ul>
<li>Call <a class="el" href="group__group__crypto__lld__sha__functions.html#ga3023d5535203528b0ef7136c86f8a66c">Cy_Crypto_Core_Sha</a> with appropriate parameters to make an operation</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line"></div><div class="line">    uint8_t sha256PlainText[3]  = <span class="stringliteral">&quot;abc&quot;</span>;</div><div class="line">    uint8_t sha256Digest[<a class="code" href="group__group__crypto__cli__srv__macros.html#ga82ff1a7671e4f0cf2026cbda5ef7e12a">CY_CRYPTO_SHA256_DIGEST_SIZE</a>] = {0};</div><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line">    <span class="comment">/* Hash the message by SHA256 */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__sha__functions.html#ga3023d5535203528b0ef7136c86f8a66c">Cy_Crypto_Core_Sha</a>(</div><div class="line">                             CRYPTO_HW,        <span class="comment">/* Pointer to Crypto instance */</span></div><div class="line">                             sha256PlainText,          <span class="comment">/* Pointer to message */</span></div><div class="line">                             <span class="keyword">sizeof</span>(sha256PlainText),        <span class="comment">/* Message size */</span></div><div class="line">                             sha256Digest,       <span class="comment">/* Pointer to digest buffer */</span></div><div class="line">                             <a class="code" href="group__group__crypto__enums.html#gga66ead9efce36e261e978cad722bf2dbba2212390919051b39d22d567c508c6d1f">CY_CRYPTO_MODE_SHA256</a>);            <span class="comment">/* Hash mode */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_Use_RSA_VER"></a>
RSA signature verification</h2>
<p>To verify the RSA signature of the data image:</p><ul>
<li>Fill RSA public key structure by RSA public key data</li>
<li>Use <a class="el" href="group__group__crypto__lld__sha__functions.html#ga3023d5535203528b0ef7136c86f8a66c">Cy_Crypto_Core_Sha</a> to calculate SHA digest of the data image</li>
<li>Use <a class="el" href="group__group__crypto__lld__asymmetric__functions.html#gae83f35c8c5a35e9b7321ada249d40d95">Cy_Crypto_Core_Rsa_Proc</a> to decrypt present encrypted signature</li>
<li>Use <a class="el" href="group__group__crypto__lld__asymmetric__functions.html#gae8aee459272ad30568e9c13a4c5dc729">Cy_Crypto_Core_Rsa_Verify</a> to verify the decrypted signature with calculated SHA digest</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">    #define RSA_MODULO_LENGTH       2048u</span></div><div class="line"><span class="preprocessor">    #define RSA_MODULO_DATA_SIZE    (RSA_MODULO_LENGTH / 8)</span></div><div class="line"></div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">struct</span></div><div class="line">    {</div><div class="line">        <a class="code" href="structcy__stc__crypto__rsa__pub__key__t.html">cy_stc_crypto_rsa_pub_key_t</a> publicKeyStruct;</div><div class="line">        uint8_t  moduloData[RSA_MODULO_DATA_SIZE];</div><div class="line">        uint8_t  expData   [32];</div><div class="line">        uint8_t  k1Data    [RSA_MODULO_DATA_SIZE+4];</div><div class="line">        uint8_t  k2Data    [RSA_MODULO_DATA_SIZE];</div><div class="line">        uint8_t  k3Data    [RSA_MODULO_DATA_SIZE];</div><div class="line">    } cy_stc_public_key_t;</div><div class="line"></div><div class="line">    <span class="comment">/* All data arrays should be 4-byte aligned */</span></div><div class="line">    cy_stc_public_key_t cy_publicKey =</div><div class="line">    {</div><div class="line">        .publicKeyStruct =</div><div class="line">        {</div><div class="line">            .moduloPtr          = 0,</div><div class="line">            .moduloLength       = RSA_MODULO_LENGTH,</div><div class="line">            .pubExpPtr          = 0,</div><div class="line">            .pubExpLength       = 24,</div><div class="line">            .barretCoefPtr      = NULL,</div><div class="line">            .inverseModuloPtr   = NULL,</div><div class="line">            .rBarPtr            = NULL</div><div class="line">        },</div><div class="line">        .moduloData =</div><div class="line">        {   <span class="comment">/* modulus in Little Endian for a public key - rsa_public.txt */</span></div><div class="line">            0xD9u, 0x94u, 0x94u, 0x38u, 0xA4u, 0xE0u, 0x50u, 0xA1u,</div><div class="line">            0xADu, 0xC5u, 0xE2u, 0x66u, 0xA9u, 0x7Fu, 0xE7u, 0xD7u,</div><div class="line">            0xA8u, 0x10u, 0x87u, 0x3Au, 0xBEu, 0xB3u, 0x0Eu, 0x6Au,</div><div class="line">            0xB2u, 0x8Bu, 0x2Eu, 0x8Du, 0xC2u, 0x45u, 0x41u, 0xA6u,</div><div class="line">            0xDBu, 0xEBu, 0x90u, 0x20u, 0x56u, 0xECu, 0xFDu, 0x8Cu,</div><div class="line">            0x23u, 0x09u, 0x13u, 0x5Du, 0x65u, 0xA2u, 0xADu, 0x9Du,</div><div class="line">            0x3Bu, 0xF2u, 0x5Bu, 0xF6u, 0xABu, 0x2Eu, 0xFEu, 0xF2u,</div><div class="line">            0x69u, 0x98u, 0x29u, 0x6Du, 0xD1u, 0x2Eu, 0x91u, 0x5Au,</div><div class="line">            0x65u, 0x83u, 0xF5u, 0x7Fu, 0x8Eu, 0x73u, 0xFFu, 0xA1u,</div><div class="line">            0x8Cu, 0x70u, 0x07u, 0xDFu, 0x4Du, 0xF4u, 0x79u, 0xB7u,</div><div class="line">            0x18u, 0xC1u, 0xA3u, 0x2Bu, 0x82u, 0x5Bu, 0x9Eu, 0xE2u,</div><div class="line">            0xF0u, 0xA0u, 0xB8u, 0xDAu, 0x19u, 0xADu, 0xBBu, 0x2Bu,</div><div class="line">            0xD5u, 0x07u, 0x5Au, 0x85u, 0x12u, 0x03u, 0xD8u, 0x60u,</div><div class="line">            0x53u, 0x3Du, 0xC6u, 0x34u, 0xE2u, 0x7Fu, 0x96u, 0x4Cu,</div><div class="line">            0x26u, 0x1Eu, 0x82u, 0xB1u, 0x85u, 0xC3u, 0x0Du, 0x54u,</div><div class="line">            0x68u, 0x37u, 0x97u, 0x58u, 0x19u, 0x36u, 0x43u, 0x9Cu,</div><div class="line">            0xD9u, 0xC3u, 0x42u, 0xEBu, 0xBEu, 0xE2u, 0x8Fu, 0x72u,</div><div class="line">            0xF1u, 0x5Eu, 0x2Au, 0x15u, 0x56u, 0x52u, 0xD4u, 0x6Du,</div><div class="line">            0x61u, 0x97u, 0x16u, 0xFEu, 0xC3u, 0xF9u, 0x17u, 0x3Cu,</div><div class="line">            0x37u, 0xD5u, 0xE1u, 0xA1u, 0x0Au, 0xB7u, 0xD9u, 0x65u,</div><div class="line">            0xA1u, 0x15u, 0xECu, 0xC7u, 0x39u, 0xECu, 0xEDu, 0x39u,</div><div class="line">            0x98u, 0x96u, 0x66u, 0x50u, 0x8Cu, 0x25u, 0xC3u, 0x29u,</div><div class="line">            0xB9u, 0xF8u, 0x25u, 0x55u, 0x92u, 0x7Au, 0xBFu, 0xFBu,</div><div class="line">            0x45u, 0x2Au, 0x28u, 0x8Au, 0xF9u, 0xE5u, 0xE2u, 0x30u,</div><div class="line">            0x72u, 0x0Eu, 0x0Au, 0x1Cu, 0x25u, 0x09u, 0x86u, 0x6Fu,</div><div class="line">            0xF6u, 0x6Fu, 0x15u, 0xEDu, 0x14u, 0xE6u, 0x1Eu, 0x53u,</div><div class="line">            0x5Au, 0x15u, 0x25u, 0xB9u, 0x5Eu, 0xC9u, 0xBAu, 0x48u,</div><div class="line">            0xA3u, 0xE3u, 0x93u, 0x62u, 0x3Cu, 0x6Cu, 0x3Cu, 0x83u,</div><div class="line">            0x17u, 0x29u, 0xFBu, 0xAEu, 0x91u, 0x47u, 0xC9u, 0x41u,</div><div class="line">            0x2Fu, 0xF9u, 0x82u, 0x29u, 0x7Bu, 0xB4u, 0x5Au, 0x93u,</div><div class="line">            0x01u, 0x95u, 0xDBu, 0x08u, 0x7Eu, 0x7Bu, 0x99u, 0x1Eu,</div><div class="line">            0xD4u, 0x25u, 0xD3u, 0x2Au, 0xF4u, 0xC7u, 0x9Fu, 0xB3u,</div><div class="line">        },</div><div class="line">        <span class="comment">/* Little endian exponent for a public key - rsa_public.txt */</span></div><div class="line">        .expData = { 0x01, 0x00, 0x01 },</div><div class="line">        .k1Data  = { 0, },</div><div class="line">        .k2Data  = { 0, },</div><div class="line">        .k3Data  = { 0, },</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">/* Present encrypted signature of the image */</span></div><div class="line">    uint8_t rsaEncryptedSign[RSA_MODULO_DATA_SIZE] =</div><div class="line">    {</div><div class="line">        0x12u, 0xCEu, 0x13u, 0x83u, 0x4Fu, 0xFFu, 0x39u, 0x9Bu,</div><div class="line">        0x33u, 0xDEu, 0xDCu, 0xDBu, 0x7Cu, 0x62u, 0xA5u, 0x10u,</div><div class="line">        0x24u, 0x07u, 0xEAu, 0x4Cu, 0x04u, 0x4Au, 0xCEu, 0x7Bu,</div><div class="line">        0x8Cu, 0xD8u, 0xD4u, 0x64u, 0xD1u, 0x98u, 0xA2u, 0x33u,</div><div class="line">        0x2Eu, 0xFFu, 0x06u, 0x13u, 0xD6u, 0x5Au, 0x50u, 0x3Cu,</div><div class="line">        0xA2u, 0x5Eu, 0xE3u, 0x11u, 0x54u, 0x7Cu, 0x6Au, 0x49u,</div><div class="line">        0x39u, 0xA3u, 0x62u, 0x02u, 0x66u, 0xB0u, 0x19u, 0x82u,</div><div class="line">        0xFBu, 0x5Du, 0x15u, 0xB2u, 0x0Bu, 0xF7u, 0xECu, 0x6Cu,</div><div class="line">        0xBEu, 0xEBu, 0x04u, 0x1Fu, 0x0Bu, 0x5Bu, 0x18u, 0x0Eu,</div><div class="line">        0x96u, 0x03u, 0xC4u, 0x1Eu, 0x56u, 0xB6u, 0x1Fu, 0xF2u,</div><div class="line">        0x08u, 0x7Au, 0x81u, 0x96u, 0x86u, 0xA4u, 0x93u, 0x5Du,</div><div class="line">        0x66u, 0x63u, 0x10u, 0xD5u, 0x9Bu, 0xA9u, 0xD7u, 0x52u,</div><div class="line">        0xCDu, 0xEFu, 0x23u, 0xDCu, 0x58u, 0xC7u, 0x3Du, 0x72u,</div><div class="line">        0x3Fu, 0x09u, 0x5Eu, 0x3Bu, 0x03u, 0xF9u, 0x91u, 0x10u,</div><div class="line">        0x63u, 0x2Au, 0x56u, 0xFAu, 0xCEu, 0x8Cu, 0x8Au, 0xBFu,</div><div class="line">        0xB5u, 0xA1u, 0xA7u, 0x0Fu, 0xBBu, 0xD8u, 0xACu, 0x7Fu,</div><div class="line">        0x43u, 0x3Cu, 0xBFu, 0x11u, 0xD8u, 0xAEu, 0x55u, 0xE7u,</div><div class="line">        0x23u, 0xF8u, 0xA4u, 0xE0u, 0x56u, 0xCFu, 0x3Fu, 0x21u,</div><div class="line">        0xA9u, 0xB6u, 0x01u, 0x34u, 0xDFu, 0xB6u, 0xDCu, 0xAAu,</div><div class="line">        0x29u, 0xB0u, 0x97u, 0x9Du, 0xD9u, 0xAAu, 0x47u, 0xD4u,</div><div class="line">        0xD9u, 0x4Au, 0x97u, 0x52u, 0x3Cu, 0xB9u, 0x36u, 0xDAu,</div><div class="line">        0xF6u, 0xA6u, 0x14u, 0x97u, 0xDEu, 0xE6u, 0x76u, 0xBFu,</div><div class="line">        0x1Bu, 0x23u, 0xDBu, 0x68u, 0x66u, 0xEAu, 0x0Eu, 0xC6u,</div><div class="line">        0xD3u, 0x52u, 0x05u, 0x86u, 0x26u, 0x04u, 0x31u, 0xCEu,</div><div class="line">        0xCFu, 0x8Du, 0x13u, 0x84u, 0x81u, 0xF2u, 0x7Bu, 0xB7u,</div><div class="line">        0xDCu, 0x93u, 0x23u, 0x93u, 0xD1u, 0x0Cu, 0xF4u, 0xDFu,</div><div class="line">        0x37u, 0x44u, 0x3Du, 0xD7u, 0xAFu, 0xBFu, 0xAFu, 0x32u,</div><div class="line">        0xE7u, 0x31u, 0x50u, 0x70u, 0x62u, 0xC3u, 0xABu, 0x31u,</div><div class="line">        0x51u, 0x28u, 0x2Bu, 0x0Bu, 0x31u, 0xC6u, 0xD3u, 0x0Fu,</div><div class="line">        0x74u, 0xE6u, 0x31u, 0x21u, 0xF0u, 0xA3u, 0x40u, 0x5Du,</div><div class="line">        0xEDu, 0xFFu, 0xC9u, 0xEBu, 0x5Au, 0x65u, 0xF3u, 0xCBu,</div><div class="line">        0x67u, 0x50u, 0x62u, 0x2Du, 0x8Au, 0xEAu, 0xC3u, 0xBDu,</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">/* Previously calculated SHA-256 digest of the image */</span></div><div class="line">    uint8_t sha256Digest[<a class="code" href="group__group__crypto__cli__srv__macros.html#ga82ff1a7671e4f0cf2026cbda5ef7e12a">CY_CRYPTO_SHA256_DIGEST_SIZE</a>] =</div><div class="line">    {</div><div class="line">        0xf4u, 0xcbu, 0x29u, 0x53u, 0xadu, 0xc2u, 0xbdu, 0x49u,</div><div class="line">        0x6bu, 0x43u, 0x28u, 0x2bu, 0x63u, 0xb7u, 0x0du, 0x16u,</div><div class="line">        0xa9u, 0xd4u, 0x90u, 0x65u, 0x6eu, 0x7bu, 0x6du, 0xb7u,</div><div class="line">        0x0bu, 0x17u, 0xefu, 0x60u, 0x02u, 0x12u, 0x0fu, 0x90u</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">/* Temporary decryption buffer */</span></div><div class="line">    uint8_t rsaOutput[RSA_MODULO_DATA_SIZE];</div><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#gaf08f93b8b21962a60f119157337b2113">cy_en_crypto_rsa_ver_result_t</a> verResult;</div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line"></div><div class="line">    cy_publicKey.publicKeyStruct.moduloPtr = cy_publicKey.moduloData;</div><div class="line">    cy_publicKey.publicKeyStruct.pubExpPtr = cy_publicKey.expData;</div><div class="line"></div><div class="line">    <span class="comment">/* Reverse the signature generated by openssl, CRYPTO IP is Little-Endian */</span></div><div class="line">    <a class="code" href="group__group__crypto__lld__hw__functions.html#ga5d74ed1e6161b72e972fac828fa3aea9">Cy_Crypto_Core_InvertEndianness</a>(rsaEncryptedSign, RSA_MODULO_DATA_SIZE);</div><div class="line"></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__asymmetric__functions.html#gae83f35c8c5a35e9b7321ada249d40d95">Cy_Crypto_Core_Rsa_Proc</a>(</div><div class="line">                             CRYPTO_HW,         <span class="comment">/* Pointer to Crypto instance */</span></div><div class="line">                             &amp;cy_publicKey.publicKeyStruct, <span class="comment">/* Pointer to key */</span></div><div class="line">                             rsaEncryptedSign,        <span class="comment">/* Pointer to signature */</span></div><div class="line">                             <span class="keyword">sizeof</span>(rsaEncryptedSign),   <span class="comment">/* size of signature */</span></div><div class="line">                             rsaOutput);    <span class="comment">/* Pointer to decrypted signature */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div><div class="line">    <span class="comment">/* Reverse the decrypted signature */</span></div><div class="line">    <a class="code" href="group__group__crypto__lld__hw__functions.html#ga5d74ed1e6161b72e972fac828fa3aea9">Cy_Crypto_Core_InvertEndianness</a>(rsaOutput, RSA_MODULO_DATA_SIZE);</div><div class="line"></div><div class="line">    <span class="comment">/* Verify decrypted signature by calculated SHA digest from data image */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__asymmetric__functions.html#gae8aee459272ad30568e9c13a4c5dc729">Cy_Crypto_Core_Rsa_Verify</a>(</div><div class="line">                             CRYPTO_HW,</div><div class="line">                             &amp;verResult,    <span class="comment">/* Pointer to verification result */</span></div><div class="line">                             <a class="code" href="group__group__crypto__enums.html#gga66ead9efce36e261e978cad722bf2dbba2212390919051b39d22d567c508c6d1f">CY_CRYPTO_MODE_SHA256</a>,              <span class="comment">/* Hash mode */</span></div><div class="line">                             sha256Digest, <span class="comment">/* Pointer to hash digest of image */</span></div><div class="line">                             rsaOutput,     <span class="comment">/* Pointer to decrypted signature */</span></div><div class="line">                             <span class="keyword">sizeof</span>(rsaOutput));         <span class="comment">/* Size of signature */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (<a class="code" href="group__group__crypto__enums.html#ggaf08f93b8b21962a60f119157337b2113ae4b7e089501db168bf6ef127b8dbcb21">CY_CRYPTO_RSA_VERIFY_SUCCESS</a> == verResult)</div><div class="line">    {</div><div class="line">        <span class="comment">/* Verification OK */</span></div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        <span class="comment">/* Verification FAILED */</span></div><div class="line">    }</div><div class="line"></div></div><!-- fragment --> <h1><a class="anchor" id="group_crypto_lld_rsa_considerations"></a>
RSA Usage Considerations</h1>
<p>General RSA encryption and decryption is supported. <a class="el" href="group__group__crypto__lld__asymmetric__functions.html#gae83f35c8c5a35e9b7321ada249d40d95">Cy_Crypto_Core_Rsa_Proc</a> encrypts or decrypts data based on the parameters passed to the function. If you pass in plain text and a public key, the output is encrypted (cipher text). If you pass in cipher text and a private key, the output is decrypted (plain text).</p>
<p>One parameter for this function call is a structure that defines the key: <a class="el" href="structcy__stc__crypto__rsa__pub__key__t.html" title="All fields for the context structure are internal. ">cy_stc_crypto_rsa_pub_key_t</a>. The four modulus and exponent fields are mandatory. They represent the data for either the public or private key as appropriate.</p>
<dl class="section note"><dt>Note</dt><dd>The <b>modulus</b> and <b>exponent</b> values in the <a class="el" href="structcy__stc__crypto__rsa__pub__key__t.html">cy_stc_crypto_rsa_pub_key_t</a> must be in little-endian order.<br />
 Use the <a class="el" href="group__group__crypto__lld__hw__functions.html#ga5d74ed1e6161b72e972fac828fa3aea9">Cy_Crypto_Core_InvertEndianness</a> function to convert to or from little-endian order.</dd></dl>
<p>The remaining fields represent three pre-calculated coefficients that can reduce execution time by up to 5x. The fields are: coefficient for Barrett reduction, binary inverse of the modulus, and the result of (2^moduloLength mod modulo). These fields are optional, and can be set to NULL.</p>
<p>Calculate these coefficients with <a class="el" href="group__group__crypto__lld__asymmetric__functions.html#ga0a60b217ca4b355bc1fcafc2e1478368">Cy_Crypto_Core_Rsa_Coef</a>. Pass them in the address of the key structure with the modulus and exponent values for the key. The function returns the coefficients for the key in the key structure, replacing any previous values.</p>
<p>The RSA functionality also implements functions to decrypt a signature using a public key. This signature must follow the RSASSA-PKCS-v1_5 standard. The signature must contain a SHA digest (hash). MD2, MD4, and MD5 message digests are not supported.</p>
<p>An encrypted signature is stored as big-endian data. It must be inverted for RSA processing. To use the provided signature decryption, firmware must</p><ol type="1">
<li>Calculate the SHA digest of the data to be verified with <a class="el" href="group__group__crypto__lld__sha__functions.html#ga3023d5535203528b0ef7136c86f8a66c">Cy_Crypto_Core_Sha</a>.</li>
<li>Ensure that the RSA signature is in little-endian format. Use <a class="el" href="group__group__crypto__lld__hw__functions.html#ga5d74ed1e6161b72e972fac828fa3aea9">Cy_Crypto_Core_InvertEndianness</a>.</li>
<li>Decrypt the RSA signature with a public key, by calling <a class="el" href="group__group__crypto__lld__asymmetric__functions.html#gae83f35c8c5a35e9b7321ada249d40d95">Cy_Crypto_Core_Rsa_Proc</a>.</li>
<li>Invert the byte order of the output, to return to big-endian format. Use <a class="el" href="group__group__crypto__lld__hw__functions.html#ga5d74ed1e6161b72e972fac828fa3aea9">Cy_Crypto_Core_InvertEndianness</a>.</li>
<li>Call <a class="el" href="group__group__crypto__lld__asymmetric__functions.html#gae8aee459272ad30568e9c13a4c5dc729">Cy_Crypto_Core_Rsa_Verify</a> (which requires data in big-endian format).</li>
</ol>
<h2><a class="anchor" id="group_crypto_lld_Use_CMAC"></a>
CMAC calculation</h2>
<p>To calculate CMAC of a message:</p><ul>
<li>Place AES key into array of appropriate size</li>
<li>Call <a class="el" href="group__group__crypto__lld__mac__functions.html#ga4e225ac7b941ef415efa59b3b0c77f09">Cy_Crypto_Core_Cmac</a> with appropriate parameters to make an operation</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line"></div><div class="line">    uint8_t cmacKey[<a class="code" href="group__group__crypto__cli__srv__macros.html#ga0b49d7f139227541dbc20bcd2a1867a4">CY_CRYPTO_AES_256_KEY_SIZE</a>] =</div><div class="line">    {</div><div class="line">        0x60u, 0x3Du, 0xEBu, 0x10u, 0x15u, 0xCAu, 0x71u, 0xBEu,</div><div class="line">        0x2Bu, 0x73u, 0xAEu, 0xF0u, 0x85u, 0x7Du, 0x77u, 0x81u,</div><div class="line">        0x1Fu, 0x35u, 0x2Cu, 0x07u, 0x3Bu, 0x61u, 0x08u, 0xD7u,</div><div class="line">        0x2Du, 0x98u, 0x10u, 0xA3u, 0x09u, 0x14u, 0xDFu, 0xF4u</div><div class="line">    };</div><div class="line"></div><div class="line">    uint8_t cmac256PlainText[16] =</div><div class="line">    {</div><div class="line">        0x6Bu, 0xC1u, 0xBEu, 0xE2u, 0x2Eu, 0x40u, 0x9Fu, 0x96u,</div><div class="line">        0xE9u, 0x3Du, 0x7Eu, 0x11u, 0x73u, 0x93u, 0x17u, 0x2Au</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">/* Calculated CMAC */</span></div><div class="line">    uint8_t cmac[<a class="code" href="group__group__crypto__cli__srv__macros.html#gab966285a569cc381f21ca7ebb9918128">CY_CRYPTO_AES_BLOCK_SIZE</a>] = {0};</div><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line">    <a class="code" href="structcy__stc__crypto__aes__state__t.html">cy_stc_crypto_aes_state_t</a> aesState;</div><div class="line"></div><div class="line">    <span class="comment">/* CMAC from the text message using AES-256 */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__mac__functions.html#ga4e225ac7b941ef415efa59b3b0c77f09">Cy_Crypto_Core_Cmac</a>(</div><div class="line">                             CRYPTO_HW,         <span class="comment">/* Pointer to Crypto instance */</span></div><div class="line">                             cmac256PlainText,       <span class="comment">/* Pointer to plain text */</span></div><div class="line">                             <span class="keyword">sizeof</span>(cmac256PlainText),  <span class="comment">/* Size of plain text */</span></div><div class="line">                             cmacKey,            <span class="comment">/* Pointer to encryption key */</span></div><div class="line">                             <a class="code" href="group__group__crypto__enums.html#gga5402841d4798c0a82d41d8433c92b62fa3b323f55c81fca9b412fea5ed28bcb30">CY_CRYPTO_KEY_AES_256</a>, <span class="comment">/* Size of encryption key */</span></div><div class="line">                             cmac,              <span class="comment">/* Pointer to calculated CMAC */</span></div><div class="line">                             &amp;aesState);    <span class="comment">/* Pointer to AES state structure */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_Use_HMAC"></a>
HMAC calculation</h2>
<p>To calculate HMAC of a message:</p><ul>
<li>Place HMAC key into array of appropriate size</li>
<li>Call <a class="el" href="group__group__crypto__lld__mac__functions.html#gaca9a7ef419e6a290f9622325fb62d37e">Cy_Crypto_Core_Hmac</a> with appropriate parameters to make an operation</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line"></div><div class="line">    uint8_t hmac256Key[<a class="code" href="group__group__crypto__cli__srv__macros.html#ga0b49d7f139227541dbc20bcd2a1867a4">CY_CRYPTO_AES_256_KEY_SIZE</a>] =</div><div class="line">    {</div><div class="line">        0x00u, 0x01u, 0x02u, 0x03u, 0x04u, 0x05u, 0x06u, 0x07u,</div><div class="line">        0x08u, 0x09u, 0x0Au, 0x0Bu, 0x0Cu, 0x0Du, 0x0Eu, 0x0Fu,</div><div class="line">        0x10u, 0x11u, 0x12u, 0x13u, 0x14u, 0x15u, 0x16u, 0x17u,</div><div class="line">        0x18u, 0x19u, 0x1Au, 0x1Bu, 0x1Cu, 0x1Du, 0x1Eu, 0x1Fu</div><div class="line">    };</div><div class="line"></div><div class="line">    uint8_t hmac256PlainText[34] =</div><div class="line">    {</div><div class="line">        0x53u, 0x61u,</div><div class="line">        0x6Du, 0x70u, 0x6Cu, 0x65u, 0x20u, 0x6Du, 0x65u, 0x73u,</div><div class="line">        0x73u, 0x61u, 0x67u, 0x65u, 0x20u, 0x66u, 0x6Fu, 0x72u,</div><div class="line">        0x20u, 0x6Bu, 0x65u, 0x79u, 0x6Cu, 0x65u, 0x6Eu, 0x3Cu,</div><div class="line">        0x62u, 0x6Cu, 0x6Fu, 0x63u, 0x6Bu, 0x6Cu, 0x65u, 0x6Eu</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">/* Calculated HMAC */</span></div><div class="line">    uint8_t hmac256[<a class="code" href="group__group__crypto__cli__srv__macros.html#ga82ff1a7671e4f0cf2026cbda5ef7e12a">CY_CRYPTO_SHA256_DIGEST_SIZE</a>] = {0};</div><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line">    <span class="comment">/* HMAC from the text message using SHA256 */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__mac__functions.html#gaca9a7ef419e6a290f9622325fb62d37e">Cy_Crypto_Core_Hmac</a>(</div><div class="line">                             CRYPTO_HW,         <span class="comment">/* Pointer to Crypto instance */</span></div><div class="line">                             hmac256,           <span class="comment">/* Pointer to calculated HMAC */</span></div><div class="line">                             hmac256PlainText,       <span class="comment">/* Pointer to plain text */</span></div><div class="line">                             <span class="keyword">sizeof</span>(hmac256PlainText),  <span class="comment">/* Size of plain text */</span></div><div class="line">                             hmac256Key,         <span class="comment">/* Pointer to encryption key */</span></div><div class="line">                             <span class="keyword">sizeof</span>(hmac256Key),    <span class="comment">/* Size of encryption key */</span></div><div class="line">                             <a class="code" href="group__group__crypto__enums.html#gga66ead9efce36e261e978cad722bf2dbba2212390919051b39d22d567c508c6d1f">CY_CRYPTO_MODE_SHA256</a>);            <span class="comment">/* SHA method */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_Use_PRNG"></a>
Pseudo Random Number Generation</h2>
<p>To generate a pseudo random number:</p><ul>
<li>Use <a class="el" href="group__group__crypto__lld__rng__functions.html#ga2ed2909fd8b7fe5cba913e3154759b89">Cy_Crypto_Core_Prng_Init</a> to set required parameters,</li>
<li>Call <a class="el" href="group__group__crypto__lld__rng__functions.html#ga612915c3ba5d4148960c2454e9418d55">Cy_Crypto_Core_Prng</a>.</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line"></div><div class="line">    <span class="comment">/* Initialization seed values for Pseudo Random Generator */</span></div><div class="line"><span class="preprocessor">    #define LFSR32_INITSTATE      (0xd8959bc9)</span></div><div class="line"><span class="preprocessor">    #define LFSR31_INITSTATE      (0x2bb911f8)</span></div><div class="line"><span class="preprocessor">    #define LFSR29_INITSTATE      (0x060c31b7)</span></div><div class="line"></div><div class="line"><span class="preprocessor">    #define MAX_PRNG_VALUE        (255UL)</span></div><div class="line"></div><div class="line">    <span class="comment">/* Generated Random Number */</span></div><div class="line">    uint32_t rndNum = 0;</div><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line">    <span class="comment">/* Initialize Pseudo Random Generator */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__rng__functions.html#ga2ed2909fd8b7fe5cba913e3154759b89">Cy_Crypto_Core_Prng_Init</a>(</div><div class="line">                             CRYPTO_HW,         <span class="comment">/* Pointer to Crypto instance */</span></div><div class="line">                             LFSR32_INITSTATE,   <span class="comment">/* Seed value for first LFSR */</span></div><div class="line">                             LFSR31_INITSTATE,  <span class="comment">/* Seed value for second LFSR */</span></div><div class="line">                             LFSR29_INITSTATE);  <span class="comment">/* Seed value for third LFSR */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div><div class="line">    <span class="comment">/* Generate a Pseudo Random number in specified range */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__rng__functions.html#ga612915c3ba5d4148960c2454e9418d55">Cy_Crypto_Core_Prng</a>(</div><div class="line">                             CRYPTO_HW,</div><div class="line">                             MAX_PRNG_VALUE,       <span class="comment">/* Range of random number */</span></div><div class="line">                             &amp;rndNum); <span class="comment">/* Pointer to generated random number */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --> <h2><a class="anchor" id="group_crypto_lld_Use_TRNG"></a>
True Random Number Generation</h2>
<p>To generate a true random number:</p><ul>
<li>Call <a class="el" href="group__group__crypto__lld__rng__functions.html#gab80f93878b181bb72bc0adf31b7eda15">Cy_Crypto_Core_Trng</a> with needed parameters.</li>
</ul>
<p>Code example: </p><div class="fragment"><div class="line"></div><div class="line">    <span class="comment">/* Initialization polynomial values for True Random Generator.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">        Polynomial for programmable Galois ring oscillator.</span></div><div class="line"><span class="comment">        The polynomial is represented WITHOUT the high order bit (this bit is always assumed &#39;1&#39;).</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">        GARO 31 bit polynomial (0x04c1:1db7) = x31 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">        Polynomial for programmable Fibonacci ring oscillator.</span></div><div class="line"><span class="comment">        The polynomial is represented WITHOUT the high order bit (this bit is always assumed &#39;1&#39;).</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">        FIRO 31 bit polynomial (0x04c1:1db7) = x31 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1</span></div><div class="line"><span class="comment">    */</span></div><div class="line"><span class="preprocessor">    #define GARO31_INITSTATE      (0x04c11db7)</span></div><div class="line"><span class="preprocessor">    #define FIRO31_INITSTATE      (0x04c11db7)</span></div><div class="line"></div><div class="line"><span class="preprocessor">    #define MAX_TRNG_BIT_SIZE     (32UL)</span></div><div class="line"></div><div class="line">    <span class="comment">/* Generated Random Number */</span></div><div class="line">    uint32_t rndNum = 0;</div><div class="line"></div><div class="line">    <a class="code" href="group__group__crypto__enums.html#ga3a65158da3dc5cb6aa016ae599542aad">cy_en_crypto_status_t</a> cryptoStatus;</div><div class="line"></div><div class="line">    <span class="comment">/* Generate True Random number in a specified range */</span></div><div class="line">    cryptoStatus = <a class="code" href="group__group__crypto__lld__rng__functions.html#gab80f93878b181bb72bc0adf31b7eda15">Cy_Crypto_Core_Trng</a>(</div><div class="line">                             CRYPTO_HW,         <span class="comment">/* Pointer to Crypto instance */</span></div><div class="line">                             GARO31_INITSTATE, <span class="comment">/* Polynomial for programmable GARO */</span></div><div class="line">                             FIRO31_INITSTATE, <span class="comment">/* Polynomial for programmable FIRO */</span></div><div class="line">                             MAX_TRNG_BIT_SIZE,     <span class="comment">/* Range of random number */</span></div><div class="line">                             &amp;rndNum);  <span class="comment">/* Pointer to generated random number */</span></div><div class="line"></div><div class="line">    <span class="comment">/* ... check for errors... */</span></div><div class="line"></div></div><!-- fragment --><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="groups"></a>
API Reference</h2></td></tr>
<tr class="memitem:group__group__crypto__lld__hw"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__hw.html">Control and Status</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:group__group__crypto__lld__symmetric"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__symmetric.html">Symmetric Key Algorithms (AES, GCM, DES, TDES)</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:group__group__crypto__lld__asymmetric"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__asymmetric.html">Asymmetric Key Algorithms (RSA, ECP, ECDSA, EDDSA)</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:group__group__crypto__lld__sha"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__sha.html">Hash Operations (SHA)</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:group__group__crypto__lld__mac"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__mac.html">Message Authentication Code (CMAC, HMAC)</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:group__group__crypto__lld__kdf"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__kdf.html">Key Derivative Function (HKDF)</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:group__group__crypto__lld__crc"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__crc.html">Cyclic Redundancy Code (CRC)</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:group__group__crypto__lld__rng"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__rng.html">Random Number Generation (TRNG, PRNG)</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:group__group__crypto__lld__vu"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__vu.html">Vector Unit (VU)</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:group__group__crypto__lld__mem"><td class="memItemLeft" align="right" valign="top">&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group__crypto__lld__mem.html">Memory Streaming Functions</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part
<div id="nav-path" class="navpath">
    <ul>
        <li class="footer">
            Generated for <b>MTB CAT1 Peripheral driver library</b> by <b>Cypress Semiconductor Corporation</b>.
            All rights reserved.
        </li>
    </ul>
</div>
-->
</body>
</html>
